/**
 * 
 */
package ro.utcn.tp.taskManagement.gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;

import com.toedter.calendar.JDateChooser;

import ro.utcn.tp.taskManagement.business.ImageService;
import ro.utcn.tp.taskManagement.business.TasksManager;
import ro.utcn.tp.taskManagement.model.ImageDefs;
import ro.utcn.tp.taskManagement.model.ImagePanel;
import ro.utcn.tp.taskManagement.model.Task;
import ro.utcn.tp.taskManagement.model.User;
import ro.utcn.tp.taskManagement.utils.DateUtils;

/**
 * @author Coni
 * 
 */
public class TaskPage implements ActionListener {

	private JLabel jlbTitlu;
	private JLabel jlbDescription;
	private JLabel jlbStatus;
	private JLabel jlbOpenDate;

	private JTextArea jttaDescription;

	private JComboBox<String> jcbStatus;

	private JButton jbnSave;

	private JButton jbnCancel;

	private JButton jbnCalendar;

	private JDateChooser calendarOpenDate;

	private Container container;

	private boolean newTask;


	/** A reference to the image service. */

	private ImageService imageService;// pentru a tine imaginile

	private static TaskPage instance;
	private User user;
	private Task task;

	/**
	 * The constructor of the class. initializes the screen.
	 */
	public TaskPage(User user) {
		this.user = user;
		imageService = ImageService.getInstance();// instantiez imageservice
		newTask = true;
		createGUI();
	}
	public TaskPage(User user,Task task) {
		this.user = user;
		this.task=task;
		imageService = ImageService.getInstance();// instantiez imageservice
		newTask = true;
		createGUI();
	}

	// imagine de background
	public void createGUI() {

		container = new ImagePanel(
				imageService.getImage(ImageDefs.APPLICATION_BACKGROUND_PATH));
		container.setLayout(new GridBagLayout());
		arrangeComponents();
	}

	public void arrangeComponents() {
		jlbTitlu = new JLabel("Add/Edit Task");

		jlbDescription = new JLabel("Description:");
		jttaDescription = new JTextArea(3, 30);
		jttaDescription.setFont(jlbTitlu.getFont());
		jttaDescription.setEditable(true);

		calendarOpenDate = new JDateChooser(new Date());

		jlbStatus = new JLabel("Status:");

		jcbStatus = new JComboBox<String>();
		fillStatusComboBox();
		jlbOpenDate = new JLabel("Open date:");

		jbnSave = new JButton("Save");
		jbnCancel = new JButton("Cancel");
		jbnCalendar = new JButton("Choose date...");

		Font font = new Font("Times New Roman", Font.ITALIC, 30);
		jlbTitlu.setFont(font);

		GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
		gridBagConstraintsx01.gridx = 0;
		gridBagConstraintsx01.gridy = 0;
		gridBagConstraintsx01.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx01.gridwidth = 3;
		gridBagConstraintsx01.fill = GridBagConstraints.BOTH;
		container.add(jlbTitlu, gridBagConstraintsx01);

		GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
		gridBagConstraintsx04.gridx = 0;
		gridBagConstraintsx04.gridy = 2;
		gridBagConstraintsx04.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx04.gridwidth = 1;
		gridBagConstraintsx04.anchor = GridBagConstraints.WEST;
		container.add(jlbDescription, gridBagConstraintsx04);

		GridBagConstraints gridBagConstraintsx05 = new GridBagConstraints();
		gridBagConstraintsx05.gridx = 1;
		gridBagConstraintsx05.gridy = 2;
		gridBagConstraintsx05.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx05.gridwidth = 1;
		gridBagConstraintsx05.gridheight = 3;
		gridBagConstraintsx05.fill = GridBagConstraints.BOTH;
		container.add(jttaDescription, gridBagConstraintsx05);

		GridBagConstraints gridBagConstraintsx06 = new GridBagConstraints();
		gridBagConstraintsx06.gridx = 0;
		gridBagConstraintsx06.gridy = 5;
		gridBagConstraintsx06.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx06.gridwidth = 1;
		gridBagConstraintsx06.anchor = GridBagConstraints.WEST;
		container.add(jlbStatus, gridBagConstraintsx06);

		GridBagConstraints gridBagConstraintsx07 = new GridBagConstraints();
		gridBagConstraintsx07.gridx = 1;
		gridBagConstraintsx07.gridy = 5;
		gridBagConstraintsx07.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx07.gridwidth = 1;
		gridBagConstraintsx07.gridheight = 1;
		gridBagConstraintsx07.fill = GridBagConstraints.BOTH;
		container.add(jcbStatus, gridBagConstraintsx07);

		GridBagConstraints gridBagConstraintsx08 = new GridBagConstraints();
		gridBagConstraintsx08.gridx = 0;
		gridBagConstraintsx08.gridy = 6;
		gridBagConstraintsx08.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx08.gridwidth = 1;
		gridBagConstraintsx08.anchor = GridBagConstraints.WEST;
		container.add(jlbOpenDate, gridBagConstraintsx08);

		GridBagConstraints gridBagConstraintsx09 = new GridBagConstraints();
		gridBagConstraintsx09.gridx = 1;
		gridBagConstraintsx09.gridy = 6;
		gridBagConstraintsx09.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx09.gridwidth = 1;
		gridBagConstraintsx09.fill = GridBagConstraints.BOTH;
		container.add(calendarOpenDate, gridBagConstraintsx09);

		GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
		gridBagConstraintsx10.gridx = 0;
		gridBagConstraintsx10.gridy = 8;
		gridBagConstraintsx10.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx10.gridwidth = 1;
		gridBagConstraintsx10.fill = GridBagConstraints.BOTH;
		container.add(jbnSave, gridBagConstraintsx10);

		GridBagConstraints gridBagConstraintsx11 = new GridBagConstraints();
		gridBagConstraintsx11.gridx = 1;
		gridBagConstraintsx11.gridy = 8;
		gridBagConstraintsx11.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx11.gridwidth = 1;
		gridBagConstraintsx11.fill = GridBagConstraints.BOTH;
		container.add(jbnCancel, gridBagConstraintsx11);

		jbnSave.setEnabled(true);
		jbnCancel.setEnabled(true);
		jbnSave.addActionListener(this);
		jbnCancel.addActionListener(this);

	}

	private void fillStatusComboBox() {
		jcbStatus.addItem(Task.TO_DO_STATUS);
		jcbStatus.addItem(Task.IN_PROGRESS_STATUS);
		jcbStatus.addItem(Task.DONE_STATUS);
		jcbStatus.addItem(Task.CANCELED_STATUS);

	}

	public void stateChanged(ChangeEvent arg0) {

	}

	public static TaskPage getNewInstance(User user) {

		instance = new TaskPage(user);

		return instance;
	}
	public static TaskPage getNewInstance(User user,Task task) {

		instance = new TaskPage(user,task);

		return instance;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		try {
			if (e.getSource() == jbnSave) {
				saveTask();
			} else if (e.getSource() == jbnCancel) {
				cancel();
			} else if (e.getSource() == jbnCalendar) {
				popUpCalendar();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void popUpCalendar() {
		// TODO Auto-generated method stub

	}

	private void saveTask() {
		// validez datele de intrare
		String taskDescription = jttaDescription.getText().trim();
		if (taskDescription.isEmpty()) {
			// afiseaza mesaj ca nu s-a introdus numele produsului
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Va rugam introduceti o descriere!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}

		// task.setClosedDate();

		boolean success = false;
		if (newTask) {
			// sunt pe pagina de adaugare task
			Task task = new Task();
			task.setDescription(jttaDescription.getText());
			task.setScheduleDate(DateUtils.initializeDate(calendarOpenDate.getCalendar()));
			task.setUserId(user.getId());
			task.setStatus((String) jcbStatus.getSelectedItem());
			success = TasksManager.getInstance().saveTask(task);

		} else {
			// sunt pe pagina de editare task
			task.setDescription(jttaDescription.getText());
			task.setScheduleDate(DateUtils.initializeDate(calendarOpenDate.getCalendar()));
			task.setUserId(user.getId());
			task.setStatus((String) jcbStatus.getSelectedItem());
			success = TasksManager.getInstance().saveTask(task);
			

		}
		if (success) {
			ApplicationScreen.getInstance().setContainer(
					UserPage.getInstance().getContainer());
		}else{
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Nu se poate salva/edita acest task!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}

	}

	public void editTask(User user,Task task) {
		newTask = false;
		jttaDescription.setText(task.getDescription());
		calendarOpenDate.setCalendar(task.getScheduleDate());

	}

	private void cancel() {
		ApplicationScreen.getInstance().setContainer(
				UserPage.getInstance().getContainer());
	}

	/**
	 * @return
	 */
	public Container getContainer() {
		return container;
	}

}
